//
//  SNImageCell.m
//  DayDayHappy
//
//  Created by yutao on 14-2-21.
//  Copyright (c) 2014年 yutao. All rights reserved.
//

#import "SNImageCell.h"

#define  kCommentBtnImage   @"comment.png"
#define  kShareBtnImage     @"share.png"



@interface SNImageCell ()

@property(strong,nonatomic)NSIndexPath *indexPath;

@end


@implementation SNImageCell

@synthesize delegate;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapAction)];
        [contentImageView addGestureRecognizer:tapGesture];
        
        [updateTime setBackgroundColor:[UIColor whiteColor]];
        [updateTime setTextColor:[UIColor colorWithRed:83/255.0 green:83/255.0 blue:83/255.0 alpha:1.0]];
        [updateTime setFont:[UIFont systemFontOfSize:12]];
        [updateTime setText:@""];
        [self addSubview:updateTime];
        
        commentBtn = [[SNShareButton alloc] initWithFrame:CGRectZero];
        [commentBtn setImage:[UIImage imageNamed:kCommentBtnImage] forState:UIControlStateNormal];
        [commentBtn addTarget:self action:@selector(commentButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:commentBtn];
        
        commentCount = [[UILabel alloc] init];
        [commentCount setTextAlignment:NSTextAlignmentLeft];
        [commentCount setBackgroundColor:[UIColor whiteColor]];
        [commentCount setText:@"8888"];
        [commentCount setTextColor:kHappTextClolr];
        [commentCount setFont:[UIFont systemFontOfSize:15]];
        [commentCount setAdjustsFontSizeToFitWidth:YES];
        [self addSubview:commentCount];
        
        shareBtn = [[SNShareButton alloc] initWithFrame:CGRectZero];
        [shareBtn setImage:[UIImage imageNamed:kShareBtnImage] forState:UIControlStateNormal];
        [shareBtn addTarget:self action:@selector(shareButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:shareBtn];
        
    }
    return self;
}


- (void)setUpImageCellWithObject:(SNImageModel*)imageModel atIndexPath:(NSIndexPath*)indexPath
{
    [super setUpImageCellWithObject:imageModel atIndexPath:indexPath];
    self.indexPath = indexPath;
    
    [contentImageView setFrame:CGRectMake(10, CGRectGetMinY(txtContent.frame) + CGRectGetHeight(txtContent.frame) +10, kCellContentWidth, ceilf(imageModel.source_height * (kCellContentWidth / imageModel.source_width)))];
    [contentImageView setImageWithURL:[NSURL URLWithString:imageModel.image] placeholderImage:[UIImage imageNamed:@""] options:SDWebImageRefreshCached];

    [updateTime setFrame:CGRectMake(10, CGRectGetMinY(contentImageView.frame) + CGRectGetHeight(contentImageView.frame) +20, 160, 25)];
    [updateTime setText:imageModel.created_at];
    
    [commentBtn setFrame:CGRectMake(170, CGRectGetMinY(contentImageView.frame) +CGRectGetHeight(contentImageView.frame), 46, 46)];
    
    [commentCount setText:[NSString stringWithFormat:@"%d",imageModel.comment_count]];
    [commentCount setFrame:CGRectMake(CGRectGetMaxX(commentBtn.frame)+ 3, CGRectGetMinY(commentBtn.frame), 40, 44)];
    
    [shareBtn setFrame:CGRectMake(CGRectGetMaxX(commentCount.frame) +4, CGRectGetMaxY(contentImageView.frame) , 46, 46)];
    
    [self setFrame:CGRectMake(10, 0, kCellContentWidth, CGRectGetMinY(shareBtn.frame) + CGRectGetHeight(shareBtn.frame) + 10)];
}


- (void)imageTapAction
{
    if ([self.delegate respondsToSelector:@selector(imageCell:didSelectRowAtIndexPath:)]) {
        [self.delegate imageCell:self didSelectRowAtIndexPath:self.indexPath];
    }
}

-(void)commentButtonClickAction
{
    if ([self.delegate respondsToSelector:@selector(commentButton:didSelectRowAtIndexPath:)]) {
        [self.delegate commentButton:commentBtn didSelectRowAtIndexPath:self.indexPath];
    }
}

-(void)shareButtonClickAction
{
    if ([self.delegate respondsToSelector:@selector(shareButton:didSelectRowAtIndexPath:)]) {
        [self.delegate shareButton:shareBtn didSelectRowAtIndexPath:self.indexPath];
    }
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
